package com.crm.system.service.impl.internalTrainingManager;

import com.crm.common.util.CommonUtils;
import com.crm.model.entity.internalTrainingManager.CustomerUser;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.systemManager.SysParameterSetting;
import com.crm.service.internalTrainingManager.CustomerUserDbService;
import com.crm.service.systemManager.ParameterSettingDbService;
import com.crm.system.service.internalTrainingManager.CustomerUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 内训部领取客户
 * @Author renshaorong
 * @Date 2021/9/6
 */
@Service
public class CustomerUserServiceImpl implements CustomerUserService {

    @Autowired
    CustomerUserDbService customerUserDbService;

    @Autowired
    ParameterSettingDbService parameterSettingDbService;


    /**
     * 领取客户
     * @param customerId
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public String receiveCustomerForInT(String customerId,String ownerId) {
        if(StringUtils.isEmpty(ownerId)){
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
            ownerId = userAccount.getAccountId();
        }

        //1.判断该客户是否被领取
        List<CustomerUser> customerUserList = customerUserDbService.selectByCustomerAndOwner(customerId,null);
        if(customerUserList.size() > 0){
            return "I00002"; //该客户已被领取
        }

        //2.判断当前账号是否超出200
        Integer integer = customerUserDbService.selectCountByOwner(ownerId);
        if(integer >= 200){
            return "I00001"; //超出200客户上限，无法领取
        }

        //3.插入数据
        CustomerUser customerUser = new CustomerUser();
        customerUser.setCustomerId(customerId);
        customerUser.setOwnerId(ownerId);
        customerUser.setCreatePeopleId(ownerId);
        customerUser.setCreateTm(new Date());
        customerUser.setDeleteStatus((short)0);
        customerUser.setCustomerUserId(CommonUtils.getGUID());
        return customerUserDbService.insertSelective(customerUser) ? "000000" : "-1";
    }

    /**
     * 释放
     * @param customerId
     * @return
     */
    @Override
    public boolean releaseCustomerForInT(String customerId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String ownerId = userAccount.getAccountId();

        //1. 判断该客户是否被领取
        List<CustomerUser> customerUserList = customerUserDbService.selectByCustomerAndOwner(customerId,ownerId);
        if(customerUserList.size() > 0){// 已领取，可释放
            CustomerUser customerUser = customerUserList.get(0);

            //2. 查询系统设置的释放限制时间
            SysParameterSetting sysParameterSetting = parameterSettingDbService.selectSystemParameterSetting();
            Integer days = sysParameterSetting.getInternalTrainingRelease();
            if (StringUtils.isEmpty(days.toString())) days = 7;

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(customerUser.getCreateTm());
            calendar.add(Calendar.DATE, days);
            Date calendarTime = calendar.getTime();
            if(calendarTime.compareTo(new Date()) > 0){
                return false;
            }

            //3. 修改删除状态
            customerUser.setDeleteStatus((short)1);
            customerUser.setUpdatePeopleId(ownerId);
            customerUser.setUpdateTm(new Date());
            return customerUserDbService.updateByPrimaryKeySelective(customerUser);
        }
        return false;
    }
}